// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, numericLiterals, spellCheckIdentifiers, spellCheckStringLiterals, com.instantiations.assist.eclipse.analysis.unusedReturnValue
/**
 * Package domain
 */
package teamrocket.domain; // $codepro.audit.disable packagePrefixNamingConvention

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import teamrocket.db.DBWrapper;

/**
 * This class creates a Patient Invoice.
 * @author James Zhang, Juan Llanes, Stephen Tapper, Will Barr
 * @version 1.0
 */
public class PatientInvoice implements IReport {

/**
 * Method generate.
 * @param params HashMap<String,Object>
 * @see teamrocket.domain.IReport#generate(HashMap<String,Object>)
 */
@Override
public void generate(HashMap<String, Object> params) {
		final TreatmentRecord record = (TreatmentRecord) params.get("record");
		
		final Patient patient = Patient.loadPatientFromId(record.getPatientid());
		final String doctorName = record.getDoctorName();
		
		final String query = "SELECT * FROM procedures WHERE orderid = ?";
		final Collection<Procedure> procedures = new ArrayList<Procedure>();
		try {
			final ResultSet results = DBWrapper.query(query, record.getDoctorsOrders());
			while(results.next()){ // $codepro.audit.disable nullPointerDereference
				Procedure procedure = new Procedure(new Date(results.getLong("date")),
						results.getString("name"),
						results.getInt("performer"),
						results.getFloat("cost"),
						results.getInt("patientid"));
				procedures.add(procedure);
			}
			final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			final String formattedDate = sdf.format(record.getDate());
			
			final Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
			final PdfWriter writer = PdfWriter.getInstance(document,  // $codepro.audit.disable variableUsage
				new FileOutputStream((String) params.get("filepath")));
			document.open();
			
			final Font headerfont = FontFactory.getFont(FontFactory.HELVETICA,
					20, Font.BOLD, BaseColor.BLACK);
			final Paragraph header = new Paragraph(
					"Team Rocke7 Health Center", headerfont);
			document.add(header);
			
			document.add(new Paragraph(""));
			
			final Font maintext = FontFactory.getFont(
					FontFactory.HELVETICA, 12, Font.NORMAL);
			final Paragraph information = new Paragraph("Patient: " +
					patient.getLastName() + ", " + 
					patient.getFirstName() + " \tDoctor: " +
					doctorName + " \tDate: " + formattedDate, maintext);
			document.add(information);
			final PdfPTable table = new PdfPTable(2);
			
			table.addCell("Procedure Name");
			table.addCell("Cost");
			float totalCost = 0.0f;
			for(Procedure proced : procedures){
				table.addCell(proced.getProcedureName());
				table.addCell("$" + proced.getCost());
				totalCost += proced.getCost();
			}
			table.addCell("Total:");
			table.addCell("$" + totalCost);
			
			document.add(table);
			document.close(); // $codepro.audit.disable closeInFinally
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
